package com.ca.ridersclubapplication.view.bar;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ridersclubapplication.view.button.RoundButton;
import com.ca.ridersclubapplication.R;
import com.ca.ridersclubapplication.util.ImageCacheUtils;
import com.ca.ridersclubapplication.util.ScoreUtils;

public class ClubTitleBar extends ConstraintLayout {

    private String imageUrl = "";
    private String name = "";
    private String score = "0.0";

    public ImageView backView;

    public ImageView imageView;
    public TextView nameView;
    public MiniScoreProcessBar scoreProcessBar;
    public TextView scoreView;

    public Button followButton;

    public ClubTitleBar(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public ClubTitleBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public ClubTitleBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {

        View view = LayoutInflater.from(context).inflate(R.layout.club_title_bar_layout, this);

        backView = view.findViewById(R.id.iv_back);
        imageView = view.findViewById(R.id.iv);
        nameView = view.findViewById(R.id.tv_name);
        scoreProcessBar = view.findViewById(R.id.bar_score);
        scoreView = view.findViewById(R.id.tv_score);
        followButton = view.findViewById(R.id.btn_follow);

        if (attrs != null) {
            TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ClubTitleBar, 0, 0);

            imageUrl = array.getString(R.styleable.ClubTitleBar_ctb_imageUrl);
            name = array.getString(R.styleable.ClubTitleBar_ctb_name);
            score = array.getString(R.styleable.ClubTitleBar_ctb_score);

            array.recycle();
        }

        setImageUrl(imageUrl);
        setName(name);
        setScore(score);
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl == null ? "" : imageUrl;
        ImageCacheUtils.loadRoundImage(getContext(), this.imageUrl, imageView, 20);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? "" : name;
        this.nameView.setText(this.name);
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score == null ? "0.0" : score;
        if (this.score.equals("0.0")) {
            this.scoreView.setText("暂无评分");
        }
        else {
            this.scoreView.setText(this.score);
        }
        int sv = ScoreUtils.parseScore(this.score);
        this.scoreProcessBar.setScore(sv);
    }
}
